﻿using System.Web.Mvc;
using System.Web.Mvc.Filters;
namespace CodeSystem.App_Start
{
/// <summary>
        /// 验证是否登录
        /// </summary>
        public class CheckLoginAttribute : FilterAttribute, IAuthenticationFilter
        {
            public void OnAuthentication(AuthenticationContext filterContext)
            {
                //这个方法是在Action执行之前调用
                var user = filterContext.HttpContext.Session["UserName"];
                if (user == null)
                {
                    var Url = new UrlHelper(filterContext.RequestContext);
                    var url = Url.Action("Login", "User", new { area = "" });
                    filterContext.Result = new RedirectResult(url);
                }
            }

            public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
            {

            }
        }
    }
